﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Microsoft.JCI.Common.Utility;
using Microsoft.JCI.Common.Entity;
using Microsoft.JCI.Bll;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.JCI.Portal.Dispatch
{
    public partial class AgentDispatchOrderView : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        public Guid DispatchFormID
        {
            get
            {
                if (ViewState["DispatchFormID"] == null) return Guid.Empty;
                else return new Guid(ViewState["DispatchFormID"].ToString());
            }
            set
            {
                ViewState["DispatchFormID"] = value.ToString();
            }
        }

        public Guid CustomerID
        {
            get
            {
                if (ViewState["CustomerID"] == null) return Guid.Empty;
                else return new Guid(ViewState["CustomerID"].ToString());
            }
            set { ViewState["CustomerID"] = value.ToString(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();

                if (Request.QueryString["CustomerID"] != null)
                {
                    CustomerID = new Guid(Request.QueryString["CustomerID"]);

                    ucCustomerCtrlInfo.CustomerID = CustomerID;

                }

                if (Request.QueryString["DispatchFormID"] != null)
                {
                    DispatchFormID = new Guid(Request.QueryString["DispatchFormID"]);

                    LoadData();
                }

                if (CustomerID != Guid.Empty)
                {
                    GetDispatchHistory(CustomerID);
                }

                if (rblFormCategory.SelectedValue != string.Empty)
                {
                    LoadFormType(Convert.ToInt32(rblFormCategory.SelectedValue));
                }
            }
        }

        private void InitPage()
        {
            EnumeratorHelper[] enums = EnumeratorHelper.GetFieldTexts(typeof(Enumerator.FormCategory));
            foreach (EnumeratorHelper e in enums)
            {
                rblFormCategory.Items.Add(new ListItem(e.EnumDisplayText, e.EnumValue.ToString()));
            }

            rblFormCategory.SelectedValue = ((int)Enumerator.FormCategory.Maintain).ToString();

            //用户控件
            SystemBll bll = new SystemBll();
            ddlM_IssueSource.DataSource = bll.GetDictionaryListFromCache(Microsoft.JCI.Common.Utility.Enumerator.DictionaryType.MaintainIssueSource);
            ddlM_IssueSource.DataTextField = "Value";
            ddlM_IssueSource.DataValueField = "DictionaryID";
            ddlM_IssueSource.DataBind();

            pnlFormTypeMaintain.Visible = false;
            pnlFormTypeComplaint.Visible = false;
            pnlFormTypeAdvisory.Visible = false;


        }

        private void LoadData()
        {
            if (DispatchFormID != Guid.Empty)
            {
                SystemBll bll = new SystemBll();
                DisDispatchFormEntity FormEntity = bll.Get<DisDispatchFormEntity>(DispatchFormID);
                if (FormEntity != null)
                {
                    //加载客户信息
                    CustomerID = FormEntity.CustomerId;
                    ucCustomerCtrlInfo.CustomerID = FormEntity.CustomerId;
                    ucCustomerCtrlInfo.Enabled = false;

                    //加载产品信息
                    ProductCtrl1.ProductID = FormEntity.ProductID;
                    ProductCtrl1.LoadProduct();
                    ProductCtrl1.Enabled = false;

                    //加载派工单信息
                    rblFormCategory.SelectedValue = FormEntity.FormCategory.ToString();
                    if (FormEntity.M_IssueSource != Guid.Empty)
                        ddlM_IssueSource.SelectedValue = FormEntity.M_IssueSource.ToString();

                    ddlM_MaintainType.SelectedValue = FormEntity.M_MaintainType;

                    ddlM_Priority.SelectedValue = FormEntity.M_Priority;
                    txtM_PersonalityService.Text = FormEntity.M_PersonalityService;
                    txtM_HQappoint.Text = FormEntity.M_HQappoint;
                    txtM_RepairContent.Text = FormEntity.M_RepairContent;
                    txtM_Remark.Text = FormEntity.M_Remark;
                    if(FormEntity.M_DiscoverDate != DateTime.MinValue)
                        RadDatePickerM_DiscoverDate.SelectedDate = FormEntity.M_DiscoverDate;
                    txtComplaintDesc.Text = FormEntity.C_Description;
                    txtAdvisoryDesc.Text = FormEntity.A_Description;
                    if (FormEntity.ReservationDate != DateTime.MinValue)
                        RadDatePicker_ReservationDate.SelectedDate = FormEntity.ReservationDate;    
                    StringBuilder sb = new StringBuilder();
                    if (FormEntity.AllocatedBranch != "" && FormEntity.AllocatedBranch != null)
                    {
                        sb.Append("在\"");
                        if (FormEntity.AllocatedBranchDate != DateTime.MinValue)
                        {
                            sb.Append(FormEntity.AllocatedBranchDate);
                        }
                        sb.Append("\"派送给办事处\"");
                        MasterDataBll mbll = new MasterDataBll();
                        SysCompanyEntity entity = mbll.GetCompanyFromCache(FormEntity.AllocatedBranch);
                        if (entity != null)
                        {
                            sb.Append(entity.Name).Append("(").Append(entity.CompanyCode).Append(")");
                        }
                        sb.Append("\"");
                        lblBranch.Text = sb.ToString();
                    }

                    sb = new StringBuilder();
                    if (FormEntity.AllocatedVendor != "" && FormEntity.AllocatedVendor != null)
                    {
                        sb.Append("在\"");
                        if (FormEntity.AllocatedVendorDate != DateTime.MinValue)
                        {
                            sb.Append(FormEntity.AllocatedVendorDate);
                        }
                        sb.Append("\"派送给供应商\"");
                        MasterDataBll mbll = new MasterDataBll();
                        SysCompanyEntity entity = mbll.GetCompanyFromCache(FormEntity.AllocatedVendor);
                        if (entity != null)
                        {
                            sb.Append(entity.Name).Append("(").Append(entity.CompanyCode).Append(")");
                        }
                        sb.Append("\"");
                        lblVendor.Text = sb.ToString();
                    }
                }


            }
        }

        private void LoadFormType(int Type)
        {
            if (Type == (int)Enumerator.FormCategory.Maintain)
            {
                pnlFormTypeMaintain.Visible = true;
                pnlFormTypeComplaint.Visible = false;
                pnlFormTypeAdvisory.Visible = false;
            }
            else if (Type == (int)Enumerator.FormCategory.Complaint)
            {
                pnlFormTypeMaintain.Visible = false;
                pnlFormTypeComplaint.Visible = true;
                pnlFormTypeAdvisory.Visible = false;
            }
            else if (Type == (int)Enumerator.FormCategory.Advisory)
            {
                pnlFormTypeMaintain.Visible = false;
                pnlFormTypeComplaint.Visible = false;
                pnlFormTypeAdvisory.Visible = true;
            }
        }

        private void GetDispatchHistory(Guid customerID)
        {
            List<DisDispatchFormEntity> list = new DispatchBll().GetAll<DisDispatchFormEntity>(" and CustomerID = '" + customerID + "'");

            radgridFormHistoryList.DataSource = list;
            radgridFormHistoryList.DataBind();
        }

        protected void radgridFormHistoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnViewDispatch = e.Item.FindControl("lnkbtnViewDispatch") as LinkButton;
                if (lnkbtnViewDispatch != null)
                {
                    lnkbtnViewDispatch.Attributes.Add("onclick", "openMyFullWin('AgentDispatchOrderView.aspx?DispatchFormId=" + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["DispatchFormId"].ToString() + "','GendDispatchOrderView');");
                }

                Label lblFormCategory = e.Item.FindControl("lblFormCategory") as Label;

                DisDispatchFormEntity entity = e.Item.DataItem as DisDispatchFormEntity;
                if (entity != null)
                {
                    lblFormCategory.Text = EnumeratorHelper.GetFieldText((Enumerator.FormCategory)entity.FormCategory);
                }
            }
        }

        protected void rblFormCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadFormType(Convert.ToInt32(rblFormCategory.SelectedValue));
        }

        private void Save()
        {
            DispatchBll bll = new DispatchBll();
            DisDispatchFormEntity entity = new DisDispatchFormEntity();
            //客户信息
            if (CustomerID == Guid.Empty)
            {
                CustomerID = Guid.NewGuid();

                ucCustomerCtrlInfo.CustomerID = CustomerID;

                bll.InsertOrUpdate(ucCustomerCtrlInfo.CustomerEntity);
            }

            //产品信息
            MadProductEntity mProductEntity = ProductCtrl1.GetEntity();
            if (mProductEntity != null)
            {
                mProductEntity.CustomerId = CustomerID;
                bll.InsertOrUpdate(mProductEntity);
            }

            //派单信息
            if (DispatchFormID != Guid.Empty)
            {
                entity = bll.Get<DisDispatchFormEntity>(DispatchFormID);
            }
            else
            {
                DispatchFormID = entity.DispatchFormId;
                entity.CreateBy = CurrentUserEntity.Account;
                entity.CreateOn = DateTime.Now;
            }

            if (CustomerID != Guid.Empty)
            {
                entity.CustomerId = CustomerID;
            }
            if (mProductEntity != null)
            {
                entity.ProductID = mProductEntity.ProductId;
            }

            entity.FormCategory = Convert.ToInt32(rblFormCategory.SelectedValue);
            entity.M_DiscoverDate = RadDatePickerM_DiscoverDate.SelectedDate.Value;
            if (entity.FormCategory == (int)Enumerator.FormCategory.Maintain)
            {
                if (ddlM_IssueSource.SelectedValue != string.Empty)
                    entity.M_IssueSource = new Guid(ddlM_IssueSource.SelectedValue);

                entity.M_MaintainType = ddlM_MaintainType.SelectedValue;

                entity.M_Priority = ddlM_Priority.SelectedValue;
                entity.M_PersonalityService = txtM_PersonalityService.Text;
                entity.M_HQappoint = txtM_HQappoint.Text;
                entity.M_RepairContent = txtM_RepairContent.Text;
                entity.M_Remark = txtM_Remark.Text;
            }
            else if (entity.FormCategory == (int)Enumerator.FormCategory.Complaint)
            {
                entity.C_Description = txtComplaintDesc.Text;
            }
            else if (entity.FormCategory == (int)Enumerator.FormCategory.Advisory)
            { entity.A_Description = txtAdvisoryDesc.Text; }


            bll.InsertOrUpdate(entity);
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            Save();
            ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "window.close();", true);
        }

        protected void lnkbtnSaveAndNew_Click(object sender, EventArgs e)
        {
            Save();
            DispatchFormID = Guid.NewGuid();
            RadDatePickerM_DiscoverDate.SelectedDate=null;
            if (Convert.ToInt32(rblFormCategory.SelectedValue) == (int)Enumerator.FormCategory.Maintain)
            {
                txtM_PersonalityService.Text = "";
                txtM_HQappoint.Text = "";
                txtM_RepairContent.Text = "";
                txtM_Remark.Text = "";
            }
            else if (Convert.ToInt32(rblFormCategory.SelectedValue) == (int)Enumerator.FormCategory.Complaint)
            { txtComplaintDesc.Text = ""; }
            else if (Convert.ToInt32(rblFormCategory.SelectedValue) == (int)Enumerator.FormCategory.Advisory)
            { txtAdvisoryDesc.Text = ""; }

            ClientScript.RegisterStartupScript(this.GetType(), "saveandnew", "alert('保存成功');", true);
        }
    }
}
